package com.ebao.service.user.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ebao.constants.UserType;
import com.ebao.dao.ITDoctorDao;
import com.ebao.dao.ITUserDao;
import com.ebao.dao.base.IBaseJpaDao;
import com.ebao.po.doctor.TDoctor;
import com.ebao.po.user.TFollow;
import com.ebao.po.user.TUser;
import com.ebao.service.base.impl.CrudServiceImpl;
import com.ebao.service.user.IFollowService;

@Service("followService")
public class FollowServiceImpl extends CrudServiceImpl<TFollow> implements IFollowService {

	@Resource(name="followDao")
	public void setDao(IBaseJpaDao<TFollow> dao) {  
		super.setDao(dao);
	}

	@Resource
	private ITUserDao userDao;
	@Resource
	private ITDoctorDao doctorDao;
	
	@Override
	public List<TUser> getFollowUsers(TUser user) {
		String jpql = "from TUser u where u.id in (select f.follower from TFollow f where f.ownType=? and f.own=? and f.followerType=?)";
		return userDao.query(jpql, UserType.COMMON_USER, user.getId(), UserType.COMMON_USER);
	}

	@Override
	public List<TDoctor> getFollowDoctors(TUser user) {
		String jpql = "from TDoctor d where d.id in (select f.follower from TFollow f where f.ownType=? and f.own=? and f.followerType=?)";
		return doctorDao.query(jpql, UserType.COMMON_USER, user.getId(), UserType.DOCTOR);
	}

	@Override
	public List<TUser> getFollowUsers(TDoctor doctor) {
		String jpql = "from TUser u where u.id in (select f.follower from TFollow f where f.ownType=? and f.own=? and f.followerType=?)";
		return userDao.query(jpql, UserType.DOCTOR, doctor.getId(), UserType.COMMON_USER);
	}

	@Override
	public List<TDoctor> getFollowDoctors(TDoctor doctor) {
		String jpql = "from TDoctor d where d.id in (select f.follower from TFollow f where f.ownType=? and f.own=? and f.followerType=?)";
		return doctorDao.query(jpql, UserType.DOCTOR, doctor.getId(), UserType.DOCTOR);
	}

	@Override
	public List<TUser> getFollowedUsers(TUser user) {
		String jpql = "from TUser u where u.id in (select f.own from TFollow f where f.ownType=? and f.followerType=? and f.follower=?)";
		return userDao.query(jpql, UserType.COMMON_USER, UserType.COMMON_USER, user.getId());
	}

	@Override
	public List<TDoctor> getFollowedDoctors(TUser user) {
		String jpql = "from TDoctor d where d.id in (select f.own from TFollow f where f.ownType=? and f.followerType=? and f.follower=?)";
		return doctorDao.query(jpql, UserType.DOCTOR, UserType.COMMON_USER, user.getId());
	}

	@Override
	public List<TUser> getFollowedUsers(TDoctor doctor) {
		String jpql = "from TUser u where u.id in (select f.own from TFollow f where f.ownType=? and f.followerType=? and f.follower=?)";
		return userDao.query(jpql, UserType.COMMON_USER, UserType.DOCTOR, doctor.getId());
	}

	@Override
	public List<TDoctor> getFollowedDoctors(TDoctor doctor) {
		String jpql = "from TDoctor d where d.id in (select f.own from TFollow f where f.ownType=? and f.followerType=? and f.follower=?)";
		return doctorDao.query(jpql, UserType.DOCTOR, UserType.DOCTOR, doctor.getId());
	}
	
}
